package cn.itcast.service.impl;

import cn.itcast.dao.PermissionDao;
import cn.itcast.entity.PageResult;
import cn.itcast.pojo.Permission;
import cn.itcast.service.PermissionService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.stream.Stream;

@Service
public class PermissionServiceImpl implements PermissionService {

    @Autowired
    private PermissionDao permissionDao;

    @Transactional
    @Override
    public void add(Permission permission) {
        //保存权限基础数据
        permissionDao.add(permission);

    }

    @Override
    public PageResult pageQuery(Integer currentPage, Integer pageSize, String queryString) {
        PageHelper.startPage(currentPage, pageSize);
        Page<Permission> page = permissionDao.findByCondition(queryString);
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Transactional
    @Override
    public void deleteById(Integer id) {
        //判断该检查项是否被引用
        int count = permissionDao.selectPermissionReferencedCount(id);
        if(count>0){
            throw new RuntimeException("当前权限被引用，不能删除 !");
        }
        //删除权限数据
        permissionDao.deleteById(id);
    }

    @Override
    public Permission findById(Integer id) {
        return permissionDao.findById(id);
    }

    @Transactional
    @Override
    public void update(Permission permission) {
        //更新权限基础信息
        permissionDao.update(permission);

    }

    @Override
    public List<Permission> findAll() {
        return permissionDao.findAll();
    }

    @Override
    public List<Permission> findByMenuId(Integer[] menuIds) {
        return permissionDao.findByMenuId(menuIds);
    }

}
